package com.ruoyi.web.controller.prod;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.HUAWEI.Software;
import com.ruoyi.system.domain.SetModel;
import com.ruoyi.system.domain.function.ProdOrder;
import com.ruoyi.system.domain.prod.ProdPsInfoVO;
import com.ruoyi.system.domain.prod.QueryProdPsInfoDTO;
import com.ruoyi.system.service.ISetModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IProdOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工单信息Controller
 *
 * @author ruoyi
 * @date 2023-02-13
 */
@Api(tags = "生产工单api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/prod/order" )
public class ProdOrderController extends BaseController {

    private final IProdOrderService iProdOrderService;

    private final ISetModelService setModelService;

    /**
     * 查询工单信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProdOrder prodOrder)
    {
        startPage();
        LambdaQueryWrapper<ProdOrder> lqw = new LambdaQueryWrapper<ProdOrder>();
        if (StringUtils.isNotBlank(prodOrder.getOrderCode())){
            lqw.eq(ProdOrder::getOrderCode ,prodOrder.getOrderCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getMaterialCode())){
            lqw.eq(ProdOrder::getMaterialCode ,prodOrder.getMaterialCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getMaterialName())){
            lqw.like(ProdOrder::getMaterialName ,prodOrder.getMaterialName());
        }
        if (StringUtils.isNotBlank(prodOrder.getProductCode())){
            lqw.eq(ProdOrder::getProductCode ,prodOrder.getProductCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getTaskTon())){
            lqw.like(ProdOrder::getTaskTon ,prodOrder.getTaskTon());
        }
        if (StringUtils.isNotBlank(prodOrder.getLineCode())){
            lqw.eq(ProdOrder::getLineCode ,prodOrder.getLineCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getLineName())){
            lqw.like(ProdOrder::getLineName ,prodOrder.getLineName());
        }
        if (StringUtils.isNotBlank(prodOrder.getRoutingCode())){
            lqw.eq(ProdOrder::getRoutingCode ,prodOrder.getRoutingCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getRoutingName())){
            lqw.like(ProdOrder::getRoutingName ,prodOrder.getRoutingName());
        }
        if (StringUtils.isNotBlank(prodOrder.getPlanNum())){
            lqw.eq(ProdOrder::getPlanNum ,prodOrder.getPlanNum());
        }
        if (prodOrder.getStartTime() != null){
            lqw.eq(ProdOrder::getStartTime ,prodOrder.getStartTime());
        }
        if (prodOrder.getEndTime() != null){
            lqw.eq(ProdOrder::getEndTime ,prodOrder.getEndTime());
        }
        if (prodOrder.getOrderNumber() != null){
            lqw.eq(ProdOrder::getOrderNumber ,prodOrder.getOrderNumber());
        }
        if (prodOrder.getOrderState() != null){
            lqw.eq(ProdOrder::getOrderState ,prodOrder.getOrderState());
        }
        if (StringUtils.isNotBlank(prodOrder.getTeam())){
            lqw.eq(ProdOrder::getTeam ,prodOrder.getTeam());
        }
        if (StringUtils.isNotBlank(prodOrder.getRemarks())){
            lqw.eq(ProdOrder::getRemarks ,prodOrder.getRemarks());
        }
        if (StringUtils.isNotBlank(prodOrder.getPlanCode())){
            lqw.like(ProdOrder::getPlanCode ,prodOrder.getPlanCode());
        }
        lqw.orderByDesc(ProdOrder::getCreateTime);
        List<ProdOrder> list = iProdOrderService.list(lqw);
        if(list.size() > 0 ){
            list.forEach(item->{
                SetModel sm = setModelService.selectSetModelByModelCode(item.getProductCode(),item.getTaskTon());
                if(sm != null){
                    item.setProductName(sm.getModelDesLocal());
                }
            });
        }
        return getDataTable(list);
    }

    /**
     * 导出工单信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:order:export')" )
    @Log(title = "工单信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(ProdOrder prodOrder) {
        LambdaQueryWrapper<ProdOrder> lqw = new LambdaQueryWrapper<ProdOrder>(prodOrder);
        List<ProdOrder> list = iProdOrderService.list(lqw);
        ExcelUtil<ProdOrder> util = new ExcelUtil<ProdOrder>(ProdOrder. class);
        return util.exportExcel(list, "order" );
    }

    /**
     * 获取工单信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:order:query')" )
    @GetMapping(value = "/{orderId}" )
    public AjaxResult getInfo(@PathVariable("orderId" ) Long orderId) {
        return AjaxResult.success(iProdOrderService.getById(orderId));
    }

    /**
     * 新增工单信息
     */
    //@PreAuthorize("@ss.hasPermi('system:order:add')" )
    @Log(title = "工单信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProdOrder prodOrder) {
        return toAjax(iProdOrderService.save(prodOrder) ? 1 : 0);
    }

    /**
     * 修改工单信息
     */
    //@PreAuthorize("@ss.hasPermi('system:order:edit')" )
    @Log(title = "工单信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProdOrder prodOrder) {
        return toAjax(iProdOrderService.updateById(prodOrder) ? 1 : 0);
    }

    /**
     * 删除工单信息
     */
    //@PreAuthorize("@ss.hasPermi('system:order:remove')" )
    @Log(title = "工单信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}" )
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(iProdOrderService.delete(Arrays.asList(orderIds)) ? 1 : 0);
    }

    @ApiOperation(value = "工单任务令版本信息查询",response = Software.class)
    @RequestMapping(value = "/getPoVersion", method = RequestMethod.GET)
    public AjaxResult getPoVersion(@RequestParam String po,@RequestParam Long orderId) {
        AjaxResult ajaxResult = iProdOrderService.getPoVersion(po,orderId);
        return ajaxResult;
    }

    @ApiOperation(value = "修改任务令版本信息")
    @RequestMapping(value = "/updatePoVersion", method = RequestMethod.POST)
    public AjaxResult updatePoVersion(
                @RequestBody Software software
                ) {
        AjaxResult ajaxResult = iProdOrderService.updatePoVersion(software);
        return ajaxResult;
    }

    /**
     * 获取已完成列表
     * @param prodOrder
     * @return
     */
    @GetMapping("/getYwclist")
    public TableDataInfo getYwclist(ProdOrder prodOrder)
    {
        startPage();
        LambdaQueryWrapper<ProdOrder> lqw = new LambdaQueryWrapper<ProdOrder>();
        if (StringUtils.isNotBlank(prodOrder.getOrderCode())){
            lqw.eq(ProdOrder::getOrderCode ,prodOrder.getOrderCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getMaterialCode())){
            lqw.eq(ProdOrder::getMaterialCode ,prodOrder.getMaterialCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getMaterialName())){
            lqw.like(ProdOrder::getMaterialName ,prodOrder.getMaterialName());
        }
        if (StringUtils.isNotBlank(prodOrder.getProductCode())){
            lqw.eq(ProdOrder::getProductCode ,prodOrder.getProductCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getTaskTon())){
            lqw.like(ProdOrder::getTaskTon ,prodOrder.getTaskTon());
        }
        if (StringUtils.isNotBlank(prodOrder.getLineCode())){
            lqw.eq(ProdOrder::getLineCode ,prodOrder.getLineCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getLineName())){
            lqw.like(ProdOrder::getLineName ,prodOrder.getLineName());
        }
        if (StringUtils.isNotBlank(prodOrder.getRoutingCode())){
            lqw.eq(ProdOrder::getRoutingCode ,prodOrder.getRoutingCode());
        }
        if (StringUtils.isNotBlank(prodOrder.getRoutingName())){
            lqw.like(ProdOrder::getRoutingName ,prodOrder.getRoutingName());
        }
        if (StringUtils.isNotBlank(prodOrder.getPlanNum())){
            lqw.eq(ProdOrder::getPlanNum ,prodOrder.getPlanNum());
        }
        if (prodOrder.getStartTime() != null){
            lqw.eq(ProdOrder::getStartTime ,prodOrder.getStartTime());
        }
        if (prodOrder.getEndTime() != null){
            lqw.eq(ProdOrder::getEndTime ,prodOrder.getEndTime());
        }
        if (prodOrder.getOrderNumber() != null){
            lqw.eq(ProdOrder::getOrderNumber ,prodOrder.getOrderNumber());
        }
        if (StringUtils.isNotBlank(prodOrder.getTeam())){
            lqw.eq(ProdOrder::getTeam ,prodOrder.getTeam());
        }
        if (StringUtils.isNotBlank(prodOrder.getRemarks())){
            lqw.eq(ProdOrder::getRemarks ,prodOrder.getRemarks());
        }
        if (StringUtils.isNotBlank(prodOrder.getPlanCode())){
            lqw.like(ProdOrder::getPlanCode ,prodOrder.getPlanCode());
        }
        lqw.eq(ProdOrder::getOrderState,"3");
        lqw.orderByDesc(ProdOrder::getOrderId).orderByAsc(ProdOrder::getOrderState);
        List<ProdOrder> list = iProdOrderService.list(lqw);
        if(list.size() > 0 ){
            list.forEach(item->{
                SetModel sm = setModelService.selectSetModelByModelCode(item.getProductCode(),item.getTaskTon());
                if(sm != null){
                    item.setProductName(sm.getModelDesLocal());
                }
            });
        }
        return getDataTable(list);
    }

    @ApiOperation(value = "工单配送详情详情")
    @RequestMapping(value = "/psInfo", method = RequestMethod.GET)
    public TableDataInfo psInfo(QueryProdPsInfoDTO dto) {
        startPage();
        List<ProdPsInfoVO> list = iProdOrderService.psInfo(dto);
        return getDataTable(list);
    }

}
